package com.glq1218.background.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author glq1218
 */
@Configuration
public class OpenApiConfig {
    /**
     * 后台接口包路径
     */
    private static final String BACKGROUND_PACKAGE = "com.glq1218.background.controller";
    /**
     * 认证接口包路径
     */
    private static final String AUTH_PACKAGE = "com.glq1218.authentication.controller";
    /**
     * 请求头名称
     */
    private static final String REQUEST_HEADER_NAME = "Authorization";

    @Bean
    public OpenAPI customOpenApi() {

        // 鉴权组件
        SecurityScheme securityScheme = new SecurityScheme()
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")
                .bearerFormat("JWT")
                .description("jwt令牌");
        // 添加统一安全认证
        Components components = new Components()
                .addSecuritySchemes(
                        REQUEST_HEADER_NAME,
                        securityScheme
                );
        return new OpenAPI()
                .components(components)
                .externalDocs(new ExternalDocumentation()
                        .description("SpringDoc Wiki Documentation")
                        .url("https://springdoc.org/v2")
                )
                .info(new Info()
                        .title("租赁平台后台接口文档")
                        .version("1.0.0")
                        .contact(new Contact()
                                .name("glq1218")
                                .email("glq05201218@163.com")
                        )
                        .description("校园租赁平台后台管理系统接口")
                        .license(new License()
                                .name("Apache 2.0")
                                .url("https://springdoc.org")
                        )
                );
    }

    @Bean
    public GroupedOpenApi authenticationGroup() {
        return GroupedOpenApi
                .builder()
                .group("authentication")
                .packagesToScan(AUTH_PACKAGE)
                .build();
    }

    @Bean
    public GroupedOpenApi backgroundGroup() {
        return GroupedOpenApi
                .builder()
                .group("background")
                .addOperationCustomizer((operation, handlerMethod) -> {
                    operation.addSecurityItem(new SecurityRequirement().addList(REQUEST_HEADER_NAME));
                    return operation;
                })
                .packagesToScan(BACKGROUND_PACKAGE)
                .build();
    }
}

